/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ProdParameter;
import com.yami.shop.bean.model.ProdParameterLang;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.dao.ProdParameterMapper;
import com.yami.shop.service.ProdParameterLangService;
import com.yami.shop.service.ProdParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Citrus
 * @date 2021-11-01 16:50:52
 */
@Service
public class ProdParameterServiceImpl extends ServiceImpl<ProdParameterMapper, ProdParameter> implements ProdParameterService {

    @Autowired
    private ProdParameterMapper prodParameterMapper;
    @Autowired
    private ProdParameterLangService prodParameterLangService;

    @Override
    @Cacheable(cacheNames = "listParameterByProdId", key = "#prodId")
    public List<ProdParameter> listParameterByProdId(Long prodId) {
        return prodParameterMapper.listProdParameterByProdId(prodId);
    }

    @Override
    @Cacheable(cacheNames = "listParameter", key = "#prodId + '-' + #lang")
    public List<ProdParameter> listParameter(Long prodId, Integer lang) {
        return prodParameterMapper.listProdParameter(prodId, lang);
    }

    @Override
    public void removeCacheByProdId(Long prodId) {
        List<String> keys = new ArrayList<>();
        keys.add("listParameter" + Constant.UNION + prodId + "-" + LanguageEnum.LANGUAGE_EN.getLang());
        keys.add("listParameter" + Constant.UNION + prodId + "-" + LanguageEnum.LANGUAGE_ZH_CN.getLang());
        keys.add("listParameterByProdId" + Constant.UNION +  prodId);
        RedisUtil.del(keys);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndUpdateParameter(List<ProdParameter> prodParameterList, Long prodId) {
        List<ProdParameter> dbProdParameterList = listParameterByProdId(prodId);
        if (CollUtil.isEmpty(dbProdParameterList) && CollUtil.isEmpty(prodParameterList)) {
            return;
        }
        List<Long> dbIds = dbProdParameterList.stream().map(ProdParameter::getProdParameterId).collect(Collectors.toList());
        List<Long> deleteIds = new ArrayList<>();
        List<ProdParameter> changeList = new ArrayList<>();
        for (ProdParameter prodParameter : prodParameterList) {
            prodParameter.setProdId(prodId);
            Long id = prodParameter.getProdParameterId();
            if (Objects.nonNull(id) && dbIds.contains(id)) {
                changeList.add(prodParameter);
                deleteIds.add(id);
                continue;
            }
            changeList.add(prodParameter);
        }
        dbIds.removeAll(deleteIds);
        if (CollUtil.isNotEmpty(changeList)) {
            // 批量更新和保存
            saveOrUpdateBatch(changeList);
            // 语言表
            saveLang(changeList);
        }
        if (CollUtil.isNotEmpty(dbIds)) {
            removeByIds(dbIds);
            // 删除语言表
            prodParameterLangService.deleteBatch(dbIds);
        }
    }

    @Override
    public void removeByProdId(Long prodId) {
        prodParameterMapper.removeByProdId(prodId);
    }

    private void saveLang(List<ProdParameter> prodParameterList) {
        List<ProdParameterLang> langList = new ArrayList<>();
        for (ProdParameter prodParameter : prodParameterList) {
            // 删除语言表信息
            prodParameterLangService.remove(new LambdaQueryWrapper<ProdParameterLang>().eq(ProdParameterLang::getProdParameterId, prodParameter.getProdParameterId()));
            ProdParameterLang prodParameterLangCn = new ProdParameterLang();
            prodParameterLangCn.setLang(LanguageEnum.LANGUAGE_ZH_CN.getLang());
            prodParameterLangCn.setProdParameterId(prodParameter.getProdParameterId());
            prodParameterLangCn.setParameterKey(prodParameter.getParameterKey());
            prodParameterLangCn.setParameterValue(prodParameter.getParameterValue());
            langList.add(prodParameterLangCn);
            ProdParameterLang prodParameterLangEn = new ProdParameterLang();
            prodParameterLangEn.setLang(LanguageEnum.LANGUAGE_EN.getLang());
            prodParameterLangEn.setProdParameterId(prodParameter.getProdParameterId());
            prodParameterLangEn.setParameterKey(prodParameter.getParameterKeyEn());
            prodParameterLangEn.setParameterValue(prodParameter.getParameterValueEn());
            langList.add(prodParameterLangEn);
        }
        prodParameterLangService.insertBatch(langList);
    }
}
